<?php

namespace app\backend\controller;

use think\Controller;
use think\Request;
use think\Db;
use app\common\model\Reward as rewardModel;

class Reward extends Controller
{
    /**
     * 邀请佣金、推广佣金设置
     *
     * @return \think\Response
     */
    public function direct()
    {
        if(request()->isPost()){
            $invitation_commissio=input('post.invitation_commissio');//邀请佣金比例
            $promotion_commission=input('post.promotion_commission');//推广佣金
            $result = rewardModel::where('field', 'invitation_commissio')->update(['value' => $invitation_commissio]);
            $result1 = rewardModel::where('field', 'promotion_commission')->update(['value' => $promotion_commission]);
            if($result || $result1){
                $data = ['code' => 0, 'msg' => '保存成功', ];
            }else{
                $data = ['code' => 1, 'msg' => '保存失败', ];
            }
            return json($data);
        }
        $list = rewardModel::where('group',1)->select();
        $this->assign('list',$list);
        return $this->fetch();
    }
    /**
     * 邀请人数设置
     */
    public function jiandian()
    {
        if(request()->isPost()){
            $info=input('post.invite_number');
            $result = rewardModel::where('group', 2)
                ->update(['tip' => $info]);
            if($result){
                $data = ['code' => 0, 'msg' => '保存成功', ];
            }else{
                $data = ['code' => 1, 'msg' => '保存失败', ];
            }
            return json($data);
        }
        $list = rewardModel::where('group',2)->find();
        $this->assign('list',$list);
        return $this->fetch();
    }
    /**
     * 客服电话设置
     */
    public function customer()
    {
        if(request()->isPost()){
            $info=input('post.customer_service');
            $result = rewardModel::where('group', 3)
                ->update(['condition' => $info]);
            if($result){
                $data = ['code' => 0, 'msg' => '保存成功', ];
            }else{
                $data = ['code' => 1, 'msg' => '保存失败', ];
            }
            return json($data);
        }
        $list = rewardModel::where('group',3)->find();
        $this->assign('list',$list);
        return $this->fetch();
    }
    /**
     * 海报管理
     */
    public function poster()
    {
        if(request()->isPost()){
            $poster=input('post.poster');
            $default_link=input('post.default_link');
            $result = rewardModel::where('field', 'poster')->update(['condition' => $poster]);
            $result1 = rewardModel::where('field', 'default_link')->update(['condition' => $default_link]);
            if($result || $result1){
                $data = ['code' => 0, 'msg' => '保存成功', ];
            }else{
                $data = ['code' => 1, 'msg' => '保存失败', ];
            }
            return json($data);
        }
        $list = rewardModel::where('group',4)->select();
        $this->assign('list',$list);
        return $this->fetch();
    }

    /**
     * 辅导奖励设置页面
     */
    public function counseling()
    {
        if(request()->isPost()){
            $info = input('post.');
            foreach($info as $key=>$value){
                $result = rewardModel::where('field', $key)->update(['value' => $value]);
            }
            if($result){
                $data = ['code' => 0, 'msg' => '保存成功', ];
            }else{
                $data = ['code' => 1, 'msg' => '保存失败', ];
            }
            return json($data);
        }
        $list = rewardModel::where('group',2)->order('id asc')->select();
        $this->assign('list',$list);
        return $this->fetch();
    }
    /**
     * 申请奖励设置页面
     */
    public function application()
    {
        if(request()->isPost()){
            $info = input('post.');
            //print_r($info);exit;
            foreach($info as $key=>$value){
                $result = rewardModel::where('field', $key)->update(['tip' => $value[0],'condition'=>$value[1],'value'=>$value[2]]);
            }
            if($result){
                $data = ['code' => 0, 'msg' => '保存成功', ];
            }else{
                $data = ['code' => 1, 'msg' => '保存失败', ];
            }
            return json($data);
        }
        $list = rewardModel::where('group',3)->order('id asc')->select();
        $this->assign('list',$list);
        return $this->fetch();
    }


    /**
     * VIP比例
     */
    public function dividend()
    {
        if(request()->isPost()){
            $dividend7 = input('post.dividend7');
            $dividend8 = input('post.dividend8');
            $dividend9 = input('post.dividend9');
            $list = [
                ['id'=>11, 'value'=>$dividend7],
                ['id'=>12, 'value'=>$dividend8],
                ['id'=>13, 'value'=>$dividend9],
            ];
            $reward = new rewardModel;
            $result = $reward->saveAll($list);
            if($result){
                $data = ['code' => 0, 'msg' => '保存成功', ];
            }else{
                $data = ['code' => 1, 'msg' => '保存失败', ];
            }
            return json($data);
        }
        $list = rewardModel::where('group',5)->order('id asc')->select();
        $this->assign('list',$list);
        return $this->fetch();
    }
    /**
     * 倍数设置
     */
    public function multiple()
    {
        $list = rewardModel::where('group',20)->field('value')->find();
        $list1 = rewardModel::where('group',21)->field('value')->find();
        $list3 = rewardModel::where('group',9)->find();
        $this->assign('list',$list);
        $this->assign('list1',$list1);
        $this->assign('list3',$list3);
        return $this->fetch();
    }
    public function transforMul()
    {
        if(request()->isPost()){
            $info=input('post.transfor_multiple');
            $result = rewardModel::where('id', 47)
                ->update(['value' => $info]);
            if($result){
                $data = ['code' => 0, 'msg' => '保存成功', ];
            }else{
                $data = ['code' => 1, 'msg' => '保存失败', ];
            }
            return json($data);
        }
    }
    public function exchangeMul()
    {
        if(request()->isPost()){
            $info=input('post.exchange_multiple');
            $result = rewardModel::where('id', 48)
                ->update(['value' => $info]);
            if($result){
                $data = ['code' => 0, 'msg' => '保存成功', ];
            }else{
                $data = ['code' => 1, 'msg' => '保存失败', ];
            }
            return json($data);
        }
    }
    public function jifenLimit()
    {
        if(request()->isPost()){
            $info=input('post.limit_jifen');
            $result = rewardModel::where('id', 49)
                ->update(['value' => $info]);
            if($result){
                $data = ['code' => 0, 'msg' => '保存成功', ];
            }else{
                $data = ['code' => 1, 'msg' => '保存失败', ];
            }
            return json($data);
        }
    }

    /**
     * 注册送GMOD
     */
    public function registe()
    {
        if(request()->isPost()){
            $jifen = input('post.registe_jifen');
            $result = rewardModel::where('group', 7)
                ->update(['value' => $jifen]);
            if($result){
                $data = ['code' => 0, 'msg' => '保存成功', ];
            }else{
                $data = ['code' => 1, 'msg' => '保存失败', ];
            }
            return json($data);
        }
        $list = rewardModel::where('group',7)->find();
        $list2 = rewardModel::where('group',8)->find();
        $list4 = rewardModel::where('group',22)->find();
        $this->assign('list',$list);
        $this->assign('list2',$list2);
        $this->assign('list4',$list4);
        return $this->fetch();
    }
    /**
     * 推荐奖励设置
     */
    public function recommend(){
        if(request()->isPost()){
            $jifen = input('post.recommend_jifen');
            $people = input('post.recommend_people');
            $result = rewardModel::where('group', 8)
                ->update(['value' => $jifen,'tip'=>$people]);
            if($result){
                $data = ['code' => 0, 'msg' => '保存成功', ];
            }else{
                $data = ['code' => 1, 'msg' => '保存失败', ];
            }
            return json($data);
        }
    }
    /**
     * GMOBGMOD兑换比例
     */
    public function convert()
    {
        if(request()->isPost()){
            $yue_jifen = input('post.yue_jifen');
            $result = rewardModel::where('group', 9)
                ->update(['value' => $yue_jifen]);
            if($result){
                $data = ['code' => 0, 'msg' => '保存成功', ];
            }else{
                $data = ['code' => 1, 'msg' => '保存失败', ];
            }
            return json($data);
        }
    }
    //币的增长调整
    public function maxcoindets(){
        if(request()->isPost()){
            $groth = input('post.grwoth');//增长率
            $max = input('post.maxcoindets');//上限

            $coindets = new rewardModel;
            $coindets->startTrans();// 开启事务
            try{
                $coindets->where('field','growth')->update(['value'=>$groth]);
                $coindets->where('field','maxcoindets')->update(['value'=>$max]);
                // 提交事务
                $coindets->commit();
                $data = ['code' => 0, 'msg' => '保存成功', ];
            } catch (\Exception $e) {
                // 回滚事务
                $coindets->rollBack();// 事务回滚
                $data = ['code' => 1, 'msg' => '保存失败', ];
            }
            return json($data);

        }
    }
    //币的价格调整
    public function coindets(){
        if(request()->isPost()){
            $data = input('post.');

            $coindets = new Coindets;
            $coindets->startTrans();// 开启事务
            try{
                foreach($data as $key=>$val){
                    $coindets->insert(['coin_name'=>$key,'coin_price'=>$val,'coin_addtime'=>time()]);
                }
                // 提交事务
                $coindets->commit();
                $data = ['code' => 0, 'msg' => '保存成功', ];
            } catch (\Exception $e) {
                // 回滚事务
                $coindets->rollBack();// 事务回滚
                $data = ['code' => 1, 'msg' => '保存失败', ];
            }
            return json($data);

        }
        $growem=rewardModel::where('field','growth')->value('value');
        $max=rewardModel::where('field','maxcoindets')->value('value');
        $list=Coindets::order(['coin_addtime'=>'desc','cid'=>'asc'])->limit(5)->select();

        return $this->fetch('',['list'=>$list,'growem'=>$growem,'max'=>$max]);
    }
    public function get_between($input, $start, $end) {
        $substr = substr($input, strlen($start)+strpos($input, $start),(strlen($input) - strpos($input, $end))*(-1));
        return $substr;

    }
}
